package com.ocom.srvpaccess.controller;

import com.ocom.common.constant.SrvpAccessDeviceUrl;
import com.ocom.common.entity.master.EntityMjQrList;
import com.ocom.common.enums.MachEnum;
import com.ocom.common.resp.RespMachMessage;
import com.ocom.common.utils.RespUtilMach;
import com.ocom.srvpaccess.service.DeviceListService;
import com.ocom.srvpaccess.service.MjDeviceInfoService;
import com.ocom.srvpaccess.service.MjQrListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class QrController {

    @Autowired
    private MjQrListService mjQrListService;

    @Autowired
    private DeviceListService deviceListService;

    @Autowired
    private MjDeviceInfoService deviceInfoService;

    // 功能二维码（functionQr）
    @PostMapping(value = SrvpAccessDeviceUrl.URLP_ACCESSDEVICE_functionQr)
    public RespMachMessage functionQr(Long companyId, String machNo, String qrCode){
        //判断参数
        if (companyId <=0 || machNo==null|| qrCode == null){
            return RespUtilMach.error(MachEnum.PARAMS_ERROR);
        }

        //1、从总库门禁二维码表mj_qr_list查找qr_code = qrCode 、end_time > 当前系统时间的记录，
        EntityMjQrList qrList = mjQrListService.selectQrInfo(companyId, machNo, qrCode);
        //如果没有记录，返回“二维码已过期”
        if (qrList == null){
            return RespUtilMach.error(MachEnum.Qr_NOT_Date);
        }
        //2、mj_qr_list . qr_function = 1 ，mj_qr_list .device_no不为空，验证设备：
        if (qrList.getQrFunction() > 1 && qrList.getDeviceNo()!=null){
            //a)如果mj_qr_list .device_no 不等于 machNo，返回“设备编号错误”
            if (!qrList.getDeviceNo().equals(machNo)){
                return RespUtilMach.error(MachEnum.MACH_NO_ERR);
            }
            //b)如果mj_qr_list . com_id 不等于 companyId，返回“设备已被其他用户登记”
            if (!qrList.getComId().equals(companyId)){
                return RespUtilMach.error(MachEnum.Device_Registered_By_User);
            }
            //c)EndDate=当前时间 + 3年
            //修改总库mj_device_list表：
            //	update mj_device_list
            //set status=0, end_time = EndDate
            //				where com_id= companyId and device_no=machNo
            try {
                deviceListService.updateEndTime(companyId,machNo);
            }catch (Exception exception){
                exception.printStackTrace();
            }

            //
            //修改分库库mj_device_info表：
            //	update mj_device_info
            //set status=0, end_time = EndDate
            //				where com_id= companyId and device_no=machNo
            try {
                deviceInfoService.updateEndTime(companyId,machNo);
            }catch (Exception exception){
                exception.printStackTrace();
            }


            //修改总库mj_qr_list表：
            //	update mj_qr_list
            //set result_code =1, result_msg = “设备验证成功”
            //				where qr_code = qrCode
            try {
                String resultMsg = mjQrListService.updateResult(companyId, machNo, qrCode);
                return RespUtilMach.success(resultMsg);
            }catch (Exception exception){
                exception.printStackTrace();
            }


        }
        return RespUtilMach.error(MachEnum.Device_Registered_By_User);
    }
}
